require('../../assets/css/index.less')
require('./sportdata.less')
require('../../assets/js/property')
require('../../assets/font/font_3852666_7r0dooui378/iconfont.css')
const request = require('../../intils/reques')
// 定义api地址
const url = 'http://fitness.h5.itsource.cn:3701'

const echarts = require('echarts')


window.onload = () => {
  // 点击返回上一页
  $('#back').addEventListener('click', () => {
    location.href = './my.html'
  })
  // 柱状图
  function renderbar(xData, series) {
    // 初始化实例
    let myChart = echarts.init($('.bar'))

    //2 写上配置项
    let option = {
      //标题组件
      title: {
        text: '近7天运动时长',
        left: 15,
        top: 15,
      },
      //提示框组件
      tooltip: {},
      //图例组件
      legend: {},
      xAxis: {
        data: xData,
        //['11-5', '11-6', '11-7', '11-8', '11-9', '11-10', '11-11']
      },
      yAxis: {},
      //核心数据
      series: series,
    }

    //使用刚指定的配置项和数据显示图表
    myChart.setOption(option)
  }

  renderbar()
  // 饼状图
  function renderpie() {
    // 初始化实例对象
    let myChart = echarts.init($('.pie'))

    // 配置
    let option = {
      title: {
        text: '运动分类',
        subtext: '',
        left: 10,
        top: 15
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left',
        top: 100,
        left: 20,
        itemWidth: 10,
        itemHeight: 10
      },
      series:
        [
          {
            name: 'Access From',
            type: 'pie',
            radius: '50%',
            data: [
              { value: 999, name: '跑步', label: { show: false } },
              { value: 735, name: '骑行', label: { show: false } },
              { value: 580, name: '训练', label: { show: false } },

            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },

          }
        ]
    };
    //使用刚指定的配置项和数据显示图表
    myChart.setOption(option)
  }
  renderpie()

  // 获取个人数据
  function getUser() {
    request.get('/api/user/info')
      .then(res => {
        let { errno, data } = res.data
        console.log(data);
        if (errno === 0) {
          $('#img1').src = url + data.imgUrl
        }
      })
  }
  getUser()
  // 获取运动数据
  function getSport() {
    request.get('/api/exercise')
      .then(res => {
        let { errno, data } = res.data
        console.log(data);
        if (errno === 0) {
          $('#sum7Calorie').textContent = data.sum7Calorie
          $('#sum7Duration').textContent = data.sum7Duration
          $('#allcalorie').textContent = data.sum7Calorie
          $('#allday').textContent = Math.floor(data.sum7Duration / 1440)
          $('#day').textContent = Math.floor(data.sum7Duration / 1440)
          //抽离x轴数据
          let xData = data.days.map(v => v.date.slice(5))
          //抽离name
          let arr = ['count', 'sumCalorie', 'sumDuration', 'sumMeter']
          let series = arr.map(name => ({
            name: name,
            type: 'bar',
            data: data.days.map(v => v[name]),
          }))

          //渲染柱状图
          renderbar(series, xData)




        }

      })




  }
  getSport()







}